<?php
session_start();
	require_once "includes/config.php";
	require_once "includes/pagination.php";
    $itemPerPage=10;	
	
	$sg = $_REQUEST['sg2'];
	$key = $_REQUEST['key'];
	//$state = $_GET['state'];
	//$city = $_GET['city'];
	//$priceFrom = $_GET['priceFrom'];
	//$priceTo = $_GET['priceTo'];
	
	
	if(!empty($sg)) 
	{	
	
	        if(isset($_REQUEST['srch1']))
		  {
			  $search_string 	= $_REQUEST['search_string'];
			  $pro_category		= $_REQUEST['pro_category'];
			  $pro_subcategory	= $_REQUEST['pro_subcategory'];
			  
			  if($search_string!='')
				  $srch_cond.=" AND tbl_wholeseller_product.product_name like '%".$search_string."%' ";
			  if($pro_category!='')
				  $srch_cond.=" AND tbl_wholeseller_product.category_id IN (SELECT category_id FROM tbl_category where parent_id=$pro_category) ";
			  if($pro_subcategory!='')
				  $srch_cond.=" AND tbl_wholeseller_product.category_id=$pro_subcategory";
				  
			  
		  }
	 
		$sqlSelPro="SELECT *, tbl_wholeseller_product.product_name, tbl_users.user_id 
					FROM tbl_wholeseller_product, tbl_users 
					WHERE tbl_users.user_id=tbl_wholeseller_product.posted_by $srch_cond";	
	
		$searchIncludes="";
		
		$sqlSelPro .= " AND tbl_wholeseller_product.posted_by IN (select user_id from tbl_users where tbl_users.status='1') ";
				
		  if($key!='')
		     $sqlSelPro.="  AND  tbl_wholeseller_product.product_name LIKE '%$key%' ";
		     
		     if(!empty($_GET['pricebtw']))
		     {
			  if($_GET['pricebtw']=='1')
			  {
				  $sqlSelPro.=" AND tbl_wholeseller_product.product_price < 10";		
			  }
			  else if($_GET['pricebtw']=='2')
			  {
				  $sqlSelPro.=" AND tbl_wholeseller_product.product_price >= 10 AND tbl_product.product_price <= 50";	
			  }
			  else if($_GET['pricebtw']=='3')
			  {
				  $sqlSelPro.=" AND tbl_wholeseller_product.product_price >= 50 AND tbl_product.product_price <= 100";	
			  }
			  else if($_GET['pricebtw']=='4')
			  {
				  $sqlSelPro.=" AND tbl_wholeseller_product.product_price >= 100 AND tbl_product.product_price <= 500";		
			  }
			  else if($_GET['pricebtw']=='5')
			  {
				  $sqlSelPro.=" AND tbl_wholeseller_product.product_price >500";	
			  }
		     }
	
		    
		 
		/* else
		{
		$sqlSelPro="SELECT * FROM tbl_product";
		$pagelink="srch&sg";
	} */
  }
	$pLink = "srchwhole=1&sg2=$sg&key=$key";	
	
	$pos     = strripos($sqlSelPro, "where");
	$curDate = date("Y-m-d G:i:s",time());
	
	$ss = mysql_query("select googleMapKey from tbl_settings where setting_id=1") or die(mysql_error());
	$tbl_settings = mysql_fetch_array($ss);
	if($tbl_settings) $googleMapKey = $tbl_settings['googleMapKey'];  else $googleMapKey='';
	//echo $curDate;
	
	//echo $sqlSelPro;
	 $sprice="hps";
	  $stitle="Lowest Price and Shipping";
	   $cls1="";
		    $cls2="";
		    $cls3="";
		 if(!empty($_GET['sortby']))
		 {
			      if($_GET['sortby']=='lps') {
				      $sqlSelPro .= " order by tbl_wholeseller_product.product_price ASC";
				      $sprice="hps";
				      $stitle="Highest Price and Shipping";
				      
			      }
			      else if($_GET['sortby']=='hps') {
				      $sqlSelPro .= " order by tbl_wholeseller_product.product_price DESC";
				       $sprice="lps";
				       $stitle="Lowest Price and Shipping";
			      }
			      else if($_GET['sortby']=='bm') {
				      $sqlSelPro .= " order by field(tbl_wholeseller_product.product_name, '$key') DESC";
				      $cls1="select";
			      }
			      else if($_GET['sortby']=='gen') {
				      $sqlSelPro .= " order by tbl_wholeseller_product.product_id ASC";		
			      }
			      else if($_GET['sortby']=='local') {
				      $sqlSelPro .= " order by tbl_wholeseller_product.localPickup DESC";
				       $cls3="select";
			      }
			     else if($_GET['sortby']=='online') {
				      $sqlSelPro .= " order by tbl_wholeseller_product.shipping DESC";
				      $cls2="select";
			      }
			      
		 }
		 else
		 {
		     $sqlSelPro .= " order by field(tbl_wholeseller_product.product_name, '$key') DESC";
		     $cls1="select";
		 }
		 
?>
<script language="javascript">
   function addtocart(id,name,amt,sh,q,tax)
   {
	 location.href="addtocart.php?id="+id +"&name="+name+"&amt="+amt+"&shipping="+sh+"&qty=1"+"&tax="+tax;
   }
</script>
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=true&amp;oe=utf-8&amp;key=<?php echo $googleMapKey; ?>" type="text/javascript"></script>
<div id='panel'>
<?php /*?><table>
	<tr>
		<td colspan="2" valign="top">	
					
		<div style="width: 950px; float: left; margin:10px 0px 5px 0px;">
		
		
						
	   	</div>
		</td>
	</tr>
</table><?php */?>

<table width="100%" border="0">

<tr>

<td>
<div style="width: 950px; float: left; vertical-align: top;">

	<div style="width: 950px; float: left; margin:10px 0px 5px 0px;">	
			<a href="javascript:" onclick="javascript:history.back();">
			 <img src="images/arrow_back.png" style="border:none; margin:0px 5px 0px 0px;" />
			 Back to Search Results
			</a>&nbsp;&nbsp;&nbsp;|&nbsp;
			<font style="color:#1DA9E2;">
				Listed in Category:<span id="listcat"> Blue Ray &amp; DVD Player</span>
			</font>				
	</div>

	<div class="mypg_left_out">
			<table style="width:100%">
			<br>
			<tr>
				<td width="214" valign="top">
				<p class="grey2_txt" pro_ptag>
					Refine Search
				</p>
				<div class="shourtbox">
				<table width="100%" border="0">
			<?php if($offerSettings)
			{
			?>
			<tr>
				<td><table width="212"  border="0" cellpadding="0" cellspacing="0">
				  <tr>
					<td></td>
				  </tr>
				  <tr>
					<td style=" background-image:url(images/LHS_menu_top.jpg); height:10px"></td>
				  </tr>
				  <tr>
					<td height="30" style=" background-image:url(images/LHS_gradient_bg.jpg); background-repeat:repeat-y;height:100px; vertical-align:top;"><div id="category-title">Offer from <?php echo $sellerName; ?> </div>
						<br />
					  <div id="category-content"><?php echo $offer; ?></div>
					  </td>
				  </tr>
				  <tr>
					<td style=" background-image:url(images/LHS_menu_bottom.jpg); height:18px"></td>
				  </tr>
				</table></td>
			  </tr>
			  <tr>
				<td>&nbsp;</td>
			  </tr>
			  <?php } ?>
			  <tr>
				<td><form id="formShopSearch" name="formShopSearch" method="get" action="">
	    <table cellspacing="0" cellpadding="0" border="0" width="212">
	    <tbody>
		    <tr>
			    <td style="vertical-align: top;">
				    <div id="category-title">Search Within Store</div>
				    <br>
				    <div class="fulldiv">
					    <center>
					             <input type="hidden" name="srchwhole" value="1">
					            <input type="hidden" name="sg2" value="<?php echo $sg?>">
						    
						    <input type="hidden" name="key" value="<?php echo $key?>">
						    
						    <input type="text" style="width: 120px; float: left;" class="inputbg" id="key" name="key" value="<?php echo $key;?>" />
						    <input type="submit" style="height: 30px; margin: 0px 0px 0px 10px;" class="yellow_btn" value="Search" name="search" onclick="javascript:$('#srch').val('1');" />
					    
					    </center>
				    </div>
			    </td>
		    </tr>

		    <tr>
			    <td style="vertical-align: top;"><br>
				    <div id="category-title">Category</div>
				    <br>

				    
				    
				    <select onchange="getsubsearch(this.value);" class="inputbg" id="pro_category" name="pro_category" style="width:200px;">
					    <?php echo get_category_all(1); ?>
				    </select>
				    
				    <script>$('#pro_category').val(<?php echo $pro_category;?>);
				            var cat=$("#pro_category option:selected").text();
					    $("#listcat").html(cat);
				    </script>
			    </td>
		    </tr>

		    <tr>
			    <td style="vertical-align: top;"><br>
				    <div id="category-title">Sub-Category</div>
				    <br>

				    
				    <select onchange="javascript:var x=document.getElementById('formShopSearch'); x.submit();" class="inputbg" id="pro_subcategory" name="pro_subcategory" style="width:200px;">
					    
					    <?php
					    if($pro_category!='') echo get_category_all($pro_category); else echo '<option selected="selected" value="">Select Sub-Category..</option>'; 
					    ?>
				    </select>
				    <script>$('#pro_subcategory').val(<?php echo $pro_subcategory;?>);
				           
				    </script>
				    <br> <br>

			    </td>
		    </tr>
		    
		    <tr>
			    <td style="vertical-align: top;">
				    <div id="category-title">By Price</div>
				    <p><a href="index.php?<?php echo $pLink;?>&pricebtw=1">under - $10</a></p>  
				    <p><a href="index.php?<?php echo $pLink;?>&pricebtw=2">$10 - $50</a></p>  
				    <p><a href="index.php?<?php echo $pLink;?>&pricebtw=3">$50 - $100</a></p>  
				    <p><a href="index.php?<?php echo $pLink;?>&pricebtw=4">$100 - $500</a></p> 
				    <p><a href="index.php?<?php echo $pLink;?>&pricebtw=5">$500 +</a></p>       
				     </td>
		    </tr>

	    </tbody>
	    </table>
	    <input type="hidden" name="srch1" id="srch1" />
	    </form></td>
			  </tr>
			</table>
				</div>
				</td>
			</tr>
			
			<?php /*?><tr>
				<td>
					<div class="text_box2" style="text-align:center;">
					The more products you pick up the more sales you will have
					</div>
				</td>
			</tr><?php */?>
			
			</table>
	</div>
		
		

<div class="fullbox_out">

<div class="panel" style="margin: 0px 0 0 5px; width: 710px;">
	
	<div class="fulldiv" style="margin:0px 0px 0px 5px;">
		<strong class="orangehead">
			Search Results <?php if(!empty($searchFor)) echo " - ".$searchFor; ?>
		</strong>
	</div>
	
	<div class="fulldiv">
			<table align="center" width="100%" border="0">
				<tr>
				<td>
				<div class="fulldiv" style="margin:0px 0px -5px 0px;">
					<table>
					<tr>
					<td width="15%">Sort Options:</td>
					<td width="15%"><a href="index.php?<?php echo $pLink; ?>&sortby=bm" class="<?php echo $cls1?>">Best Match</a></td>
							<?php /*?><td width="27%"><a href="index.php?<?php echo $pLink; ?>&sortby=lps">Lowest Price and Shipping</a></td>
							<td width="29%"><a href="index.php?<?php echo $pLink; ?>&sortby=hps">Highest Price and Shipping</a></td><?php */?>
							<td width="15%"><a href="index.php?<?php echo $pLink; ?>&sortby=online" class="<?php echo $cls2?>">Online Only</a></td>
							<td width="15%"><a href="index.php?<?php echo $pLink; ?>&sortby=local" class="<?php echo $cls3?>">Local Only</a></td>
					</tr></table>
				</div>	</td>			
				<td width="30%">
						<div class="greytab_out" style="margin:0px 10px -12px 5px;">
							
							<div class="yellow_btn" style="cursor:pointer; padding:10px 5px 0px 5px; height:25px;" title="<?php echo $stitle?>" onclick="window.location='index.php?<?php echo $pLink;?>&sortby=<?php echo $sprice?>'">Sort price</div>
							
						</div>
					</td>
				
				</tr>
			</table>
	</div>
	 <?php
	 
	         
		// echo $sqlSelPro;
		 $rstSelPro=mysql_query(getPagingQuery($sqlSelPro,$itemPerPage));			  
		 $num=mysql_num_rows($rstSelPro);				   
		 if($num==0)
		      {					
			      ?><div class="fulldiv">
			      <div class="fullbox_top"></div>
			      <div class="fullbox_repeat">
			      <?php	echo "&nbsp;&nbsp;No Products Found...";?>
			      </div>
				      <div class="fullbox_bot"></div>
			      </div>
		      <?	
		       
		      }
		 else {
		  
		   while($rowSelPro=mysql_fetch_array($rstSelPro))
		   {							   
			$productId=$rowSelPro['product_id'];
			$postedby=$rowSelPro['posted_by'];
			$itemId=$rowSelPro['product_id'];
			$productName=wordFormater($rowSelPro['product_name'],46);
			$productTitle = $rowSelPro['product_name'];					   
			$productPrice=$rowSelPro['product_price'];
			$postedDate=$rowSelPro['posted_date'];
			$productCode=$rowSelPro['product_code'];
			$ownername=getUser($postedby);
			$shipping=$rowSelPro['shipping'];
			$localPickup=$rowSelPro['localPickup'];
			if($shipping!="Free")
			{
			   $shipping="$".$shipping;
			}
			
			if($localPickup=="1")
			{
			   $lpickup="Available";
			}
			else
			{
			    $lpickup="Not available";
			}
			$pimage = getWholeProductImage($itemId,'p');
			if($pimage['thumb']!="") {
				     $productThumb = $pimage['thumb'];
			} else {
				     $productThumb="images/no_image.gif";	
			}
			
			$qry = mysql_query("select address_line_1,address_line_2,city,state,country,zip,user_type from tbl_users where user_id=$postedby") or die(mysql_error());
			$cnt_users=mysql_num_rows($qry);
			if($cnt_users==1)
			{
			  $users = mysql_fetch_array($qry);
			  $address_line_1 	= $users['address_line_1'];
			  $address_line_2 	= $users['address_line_2'];
			  $address_line_3 	= $users['address_line_3'];
			  $city			= $users['city'];
			  $state		= $users['state'];
			  $country		= $users['country'];
			  $zip		= $users['zip'];
			  
			  $address_of_seller	= $address_line_1;
			  if($address_line_2!='')
			   $address_of_seller.=",".$address_line_2;
			  if($address_line_3!='')
			   $address_of_seller.=",".$address_line_2;
			  if($city!='')
			   $address_of_seller.=",".$city;
			  if($state!='')
			   $address_of_seller.=",".$state;
			  
			  $cqry = mysql_query("select country_name from tbl_country where country_id=$country") or die(mysql_error());
			  $tbl_country = mysql_fetch_array($cqry);
			  if($tbl_country)
			  {
			   $address_of_seller.=",".$tbl_country['country_name'];
			  }
			  
			  if($zip!='')
			  {
			   $address_of_seller.=",".$zip;
			  }
			  
			  $mtype=$users['user_type'];
			}
		  
		  ?>
	<div class="fulldiv">
			<div class="fullbox_top"></div>
			<div class="fullbox_repeat">
				<div class="sellpro_table">
					<div class="sellpro_row_img">
					<img src="<?php echo $productThumb?>">
					</div>
					
					
					<div class="sellpro2_out">
					
					<div class="fulldiv">
							
						<div class="sellpro_row2" style=" width:255px; text-align:left !important;">
							<p class="sellpro_head pro_ptag">
							<a href='index.php?wholeproducts=<?php echo $itemId; ?>' title='<?php echo $productTitle; ?>' class="sellpro_head"> <?php echo $productName?> </a>
							</p>
							<p class="pro_ptag">product owner - <?php echo $ownername?></p>
						</div>
						
						
						<div class="sellpro_row3" style="text-align:center;">
						<p class="pro_ptag">
						<img src="images/w.png" />
						</p>
						<p class="pro_ptag">
							<a class="bluelink" href="#">chat available&nbsp;
							<img src="images/exclamation.png" style="border:0;" />
							</a>
						</p>
					<input type="hidden" name="address_of_seller" value="<?php echo $address_of_seller;?>" id="address_of_seller<?php echo $productId;?>" />
							     <script>
								   var geocoder, location1, location2, gDir;
								  
								   geocoder = new GClientGeocoder();
								   gDir = new GDirections();
								  </script>
							     <input type="hidden" name="lat<?php echo $productId;?>" value="" id="lat<?php echo $productId;?>" />
								<input type="hidden" name="lon<?php echo $productId;?>" value="" id="lon<?php echo $productId;?>" />
								
							     <?php
							     
							       if(!empty($_SESSION['user_id_sess']))
							       {
								 
								?>
								
								<input type="hidden" name="address_of_user" value="<?php echo $address_of_user;?>" id="address_of_user" />
								 
								<script>
									    
									    GEvent.addListener(gDir, "load", function() {
										    var drivingDistanceMiles = gDir.getDistance().meters / 1609.344;
										    var drivingDistanceKilometers = gDir.getDistance().meters / 1000;
										    //document.getElementById('results').innerHTML = '<strong>Address 1: </strong>' + location1.address + ' (' + location1.lat + ':' + location1.lon + ')<br /><strong>Address 2: </strong>' + location2.address + ' (' + location2.lat + ':' + location2.lon + ')<br /><strong>Driving Distance: </strong>' + drivingDistanceMiles + ' miles (or ' + drivingDistanceKilometers + ' kilometers)';
										    var result = drivingDistanceMiles.toFixed(2);
										    $('#distance').html('<i>miles away - </i><br /><font style="color:#000000;">'+result+' miles</font> <br />');
									    });
								  </script>
								<?
								 }
							       ?>
								<script>
								
								var address_of_seller = $('#address_of_seller<?php echo $productId;?>').val();
								geocoder.getLocations(address_of_seller, function (response) {
									if (!response || response.Status.code != 200)
									{
										//alert("Sorry, we were unable to geocode the first address");
									}
									else
									{
										
										
										location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
										
										$('#lat<?php echo $productId;?>').val(response.Placemark[0].Point.coordinates[1]);
										$('#lon<?php echo $productId;?>').val(response.Placemark[0].Point.coordinates[0]);
										<?php
									       if(!empty($_SESSION['user_id_sess']))
									       {
										?>
										var address_of_user = $('#address_of_user').val();
										geocoder.getLocations(address_of_user, function (response) {
											if (!response || response.Status.code != 200)
											{
												//alert("Sorry, we were unable to geocode the second address");
											}
											else
											{ 
												location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
												gDir.load('from: ' + location1.address + ' to: ' + location2.address);
											}
										
											
										});
										<?php } ?>
									}
								 });
																
							       </script>	

					</div>
					
						
						<div class="sellpro_row2" style="text-align:right; font-size:12px; width:180px;">
							<p style="margin: 5px 0px; font-size: 12px;" class="redtxt">
									$<?php echo number_format($productPrice,2); ?>+<font style="color: #000000;">  <?php echo $shipping; ?> S &amp; h</font>
							</p>
							<p class="pro_ptag">minimum quantity<br /><i>Per Order: 5</i></p>
						</div>
					
					</div>
					
				<div class="fulldiv" style="margin:30px 0px 0px 0px;">
					<div class="sellpro_row2" style="width:195px; margin:5px 0px 0px 0px; text-align:left !important;">
						<p class="pro_ptag">local pick up - <font style="color:#000000;"><?php echo $lpickup?></font></p>
					</div>
					
					<div class="sellpro_row3"  style="text-align:center; width:215px; margin:5px 0px 0px 0px;">
						<div class="fulldiv" align="center" style="margin-top:-27px;"><img src="images/pushpin.png" /></div>
						<p style="font-size:12px;" class="pro_ptag">
							<span id="distance"></span>&nbsp;
								<font style="color:#000000;cursor:pointer;">(<a class="bluelink" href="javascript:void(0)" onclick="show_map(<?php echo $productId;?>,'<?php echo $address_of_seller;?>')">map it</a>)</font>
							</p>
						</p>
					</div>
					
					
					<div class="sellpro_row2"  style="text-align:right; font-size:12px; width:138px; margin:0px;">
						<p class="pro_ptag"><a href="javascript:void(0);" style="font-size:14px; font-weight:bold;" onclick="return addtocart('<?php echo $itemId;?>','<?php echo $productName;?>','<?php echo $productPrice;?>','<?php echo $shipping;?>',1,'<?php echo $sales_tax;?>');">ADD TO CART <img src="images/cartnew.png" border="0" style="margin:0px 0px -7px 0px;" /></a></p>
					</div>
				</div>
					
					</div>
					
				</div>
			</div>
			<div class="fullbox_bot"></div>
	</div>
					
	<div class="fulldiv" align="center">
	  
	  <div class="fulldiv">

	
									 	
							 <?php } ?>				 
							
			<?php } ?>
		
		  <div class="fulldiv">
				<div class="fulldiv" align="center"><?php
					   $link=$pLink;
					   echo $disp=getPagingLink($sqlSelPro,$itemPerPage,$link);							   
				 ?></div>
				
			</div>
		 
	
		</div>
	</div>
	
</div>

</div> 

</div>
</td>
</tr>
</table>
</div>
<script>
function show_map(pid,address)
{
  // var pos = $('#more_info_link').position();
  var ntop = ( $(window).height() - $('#map_box').height() ) / 2+$(window).scrollTop();
  var nleft = ( $(window).width() - $('#map_box').width() ) / 2+$(window).scrollLeft();
  
  $('#map_box').css({'left': nleft, 'top':ntop});
  $('#map_box').show();
  var lat = $('#lat'+pid).val();
  var lon = $('#lon'+pid).val();
  //alert(lat+' '+lon);
  initialize(lat,lon);
 
  
}



function initialize(lat,lon) {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(lat, lon), 13);
    var latlng = new GLatLng(lat,lon);
    map.addOverlay(new GMarker(latlng));
    
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
  }
}

function getsubsearch(category_id)
{
	var url = "<?php echo $base_url;?>ajax_category_option.php";
	$.post(url,
	{ 'category_id':category_id},
	function(data)
	{
		$('#pro_subcategory').html(data);
	},
	'html');
	
	var x=document.getElementById("formShopSearch");
	x.submit();
}

</script>
<div id="map_box" style="position:absolute; width:650px; height:470px; background:#000000; display:none;" align="center">
 <span style="float:right; color:#fff; padding:10px; font-weight:bold; cursor:pointer;" onclick="$('#map_box').hide();">x</span><br />
 <div id="map_canvas" style="width: 600px; height: 400px"></div>
</div>
